//
//  RecvAddressViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/15.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "RecvAddressViewController.h"
#import "AppConfig.h"
#import "NewAddrCell.h"
#import "MBProgressHUD+YC.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "RecvAddressQueryModel.h"
#import "ModifyRecvAdrViewController.h"

@interface RecvAddressViewController ()<QXNetWorkToolDelegate,UITableViewDataSource , UITableViewDelegate>

//@property (weak, nonatomic) IBOutlet UILabel *currNameLabel;
//@property (weak, nonatomic) IBOutlet UILabel *currAddrLabel;
//@property (weak, nonatomic) IBOutlet UILabel *currPhoneNumLb;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
//@property (strong, nonatomic) UIView *messageView;
//@property (strong, nonatomic) UIView *bottomView;
@property (strong, nonatomic) RecvAddressQueryModel *model;

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NewAddrCell *myCell;
@property (assign, nonatomic) int heightCell;


@end

@implementation RecvAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    self.bottomView = [self.view viewWithTag:888];
//    
//    //手势
//    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    self.heightCell = 216;
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.myCell getHeight];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.myCell = [NewAddrCell loadFromNib];
    
    self.myCell.nameLb.enabled = NO;
    self.myCell.addrField.enabled = NO;
    self.myCell.phoneLabel.enabled = NO;
    self.myCell.mailLabel.enabled = NO;
    
    if (self.model)
    {
        self.myCell.nameLb.text = self.model.consignees_name;
        self.myCell.phoneLabel.text = self.model.consignees_phone;
        self.myCell.mailLabel.text =  self.model.postcode;
        self.myCell.addr.text = self.model.address;
        
        self.myCell.addr.hidden = YES;
        self.myCell.addrField.hidden = YES;
        
        if (![self.model.consignees_name isEqualToString:@""]
            || ![self.model.address isEqualToString:@""]
            || ![self.model.consignees_phone isEqualToString:@""]
            || ![self.model.postcode isEqualToString:@""])
        {
            //有数据
            self.myCell.addr.hidden = NO;
        }
        else
        {
            //没有数据
            self.myCell.addrField.hidden = NO;
        }
    }
    
    return self.myCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(void)tap
{
    ModifyRecvAdrViewController *obj = [ModifyRecvAdrViewController create];
    
    if (self.model)
    {
        obj.name = self.model.consignees_name;
        obj.phone = self.model.consignees_phone;
        obj.code = self.model.postcode;
        obj.address = self.model.address;
    }
    
    [self.navigationController pushViewController:obj animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self configNavigationItem];
    
    [self configData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void) configNavigationItem
{
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"收货地址";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [self configNavigationRightItemWith:[UIImage imageNamed:@"btn_recv_address"] andAction:^{
        
        [self tap];
        
    }];
}

- (void)configData
{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];
    
    tool.QXNetWorkDelegate = self;
    
    [tool GetDataFromServerWithPostMethodandInterfaceName:ReciveAddressQueryInterface andParameter:@[@{@"user_id":[UserDataSingleton shareInstance].user_id}] tag:1];
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
    
}

//- (void)GetSuccessMessage:(id)message andTag:(int)tag
//{
//    [MBProgressHUD hideHUD];
//    
//    NSString *mes = message;
//    
//    switch (tag)
//    {
//        case 1:
//            [MBProgressHUD showSuccess:mes];
//            
//            break;
//    }
//}


-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    if (Response == nil)
    {
        return;
    }
    switch (tag)
    {
        case 1:
        {
            for (NSDictionary *dic in Response)
            {
                RecvAddressQueryModel *model = [RecvAddressQueryModel RecvAddressQueryWithDict:dic];
                
                self.model = model;
                
                [self.myTableView reloadData];
                break;
            }
        }
    }
}

-(void)pressLeft:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)newAddrBtnClick:(id)sender
{
    [self.navigationController pushViewController:[ModifyRecvAdrViewController create] animated:YES];
}

@end












